package com.store.practice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.store.practice.dto.BgStaffInsertParam;
import com.store.practice.mapper.BgDepartmentMapper;
import com.store.practice.mapper.BgPositionMapper;
import com.store.practice.mapper.BgStaffMapper;
import com.store.practice.model.BgDepartment;
import com.store.practice.model.BgStaff;
import com.store.practice.model.BgStaffExample;
import com.store.practice.service.BgStaffService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BgStaffServiceImpl implements BgStaffService {
    private static final Logger logger = LoggerFactory.getLogger(BgStaffServiceImpl.class);

    @Autowired
    private BgStaffMapper staffMapper;
    @Autowired
    private BgDepartmentMapper departmentMapper;
    @Autowired
    private BgPositionMapper positionMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Value("${minio.endpoint}")
    private String ENDPOINT;
    @Value("${minio.bucketName}")
    private String BUCKET_NAME;
    @Value("${minio.accessKey}")
    private String ACCESS_KEY;
    @Value("${minio.secretKey}")
    private String SECRET_KEY;
    @Value("${minio.defaultPicture-url}")
    private String DEFAULT_PIC;

    @Override
    public int insert(BgStaffInsertParam param) {
        BgStaff staff = new BgStaff();
        BeanUtil.copyProperties(param,staff);
        staff.setPassword(passwordEncoder.encode(staff.getPassword()));
        staff.setHead(ENDPOINT+"/"+BUCKET_NAME+"/"+DEFAULT_PIC);
        int i = staffMapper.insert(staff);
        Integer dptId = param.getDptId();
        BgDepartment department = departmentMapper.selectByPrimaryKey(dptId);
        department.setPersonSum(department.getPersonSum()+1);
        logger.info("person_sum = {}",department.getPersonSum());
        departmentMapper.updateByPrimaryKey(department);
        return i;
    }

    @Override
    public int update(Integer id,BgStaffInsertParam param) {
        BgStaff staff = staffMapper.selectByPrimaryKey(id);
        Integer dptId = staff.getDptId();
        Integer newDptId = param.getDptId();
        if (!dptId.equals(newDptId)){
            BgDepartment department1 = departmentMapper.selectByPrimaryKey(dptId);
            department1.setPersonSum(department1.getPersonSum() - 1);
            BgDepartment department2 = departmentMapper.selectByPrimaryKey(newDptId);
            department2.setPersonSum(department2.getPersonSum() + 1);
            logger.info("{} has staff num:{},{} has staff num:{}", department1.getDptName(), department1.getPersonSum(), department2.getDptName(), department2.getPersonSum());
            departmentMapper.updateByPrimaryKey(department1);
            departmentMapper.updateByPrimaryKey(department2);
        }
        BeanUtil.copyProperties(param,staff);
        staff.setPassword(passwordEncoder.encode(staff.getPassword()));
        return staffMapper.updateByPrimaryKey(staff);
    }

    @Override
    public int delete(Integer id) {
        BgStaff staff = staffMapper.selectByPrimaryKey(id);
        Integer dptId = staff.getDptId();
        BgDepartment department = departmentMapper.selectByPrimaryKey(dptId);
        department.setPersonSum(department.getPersonSum()-1);
        departmentMapper.updateByPrimaryKey(department);
        return staffMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<BgStaff> list() {
        List<BgStaff> list = staffMapper.selectByExample(null);
        if (list==null||list.size()==0){
            return null;
        }else {
            return list;
        }
    }
}
